<?php
namespace Home\Controller;

//通用Controller类
class CommonController extends \Think\Controller {
    
    public $info;
    public $is_care;
    protected $openid;
    protected $fromuserName; //cookie名
    
    public function _initialize(){
        header("Content-Type:text/html;charset=utf-8");
        $this->fromuserName = C('APPID').'fromuser';
        $this->is_care = 0;//是否关注

        if (isset($_COOKIE[$this->fromuserName])) {
            //解密出openid和时间戳
            $fromuser = $_COOKIE[$this->fromuserName];
            $res = decodeOpenid($fromuser);

            if ($res['status'] == 0 || empty($res['openid'])) {
                setcookie($this->fromuserName, '', 1);
            } else {
                $this->openid = $res['openid'];

                //判断是否关注
                $userService = new \Overtrue\Wechat\User(C('APPID'), C('APPSECRET'));
                $userInfo = $userService->get($res['openid']);
                if ($userInfo['subscribe'] == 1) $this->is_care = 1;
            }
        }

        $this->info['is_care']  = $this->is_care;

        //微信分享
        if (!IS_AJAX) {
            $js = new \Overtrue\Wechat\Js(C('APPID'), C('APPSECRET'));
            $this->info['js_config'] = $js->config(array('onMenuShareTimeline', 'onMenuShareAppMessage'));
            $this->info['shareTitle'] = '上海农商银行|小红卡爱旅行';
            $this->info['shareDesc'] = '晒出最美旅行照，赢4999元旅行基金哦！';
            $this->info['shareUrl']  = U('Home/Index/index', array(), false, true); // 默认分享首页
            $this->info['shareImgUrl']  = C('IMG_PREFIX').'jsshare.png';
        }
    }
    
}
